package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommentBean;
import com.dtw.ditouwang.beans.CommonBean;
import com.dtw.ditouwang.beans.SySArtBean;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.utils.ShareUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.wechat.Wechat;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditorChooseNormalDetailActivity extends BaseActivity implements OnItemClickListener, TextView.OnEditorActionListener, View.OnClickListener {
    public static final String KeySYSID = "KeySYSID";
    TextView awesome, collection;
    WebView content;
    RecyclerView recyclerView;
    CommentAdapter commentAdapter;
    List<CommentBean.DataBean> commentList = new ArrayList<>();
    EditText editText;
    String titleText="低头网";
    String contentText = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor_choose_normal_detail);
        enableBack();
        setTitle("文章详情");
        awesome = findViewById(R.id.text_awesome);
        collection = findViewById(R.id.text_collection);
        content = findViewById(R.id.text_content);
        content.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        content.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return true;
            }
        });
//        content.getSettings().setDefaultTextEncodingName("UTF-8");
        recyclerView = findViewById(R.id.recyclerview);
        editText=findViewById(R.id.text_send_comment);
        editText.setOnEditorActionListener(this);
        commentAdapter = new CommentAdapter(commentList);
        commentAdapter.setMoreCommentListener(EditorChooseNormalDetailActivity.this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(commentAdapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        findViewById(R.id.sendButton).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        webApi.getSystemArt(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeySYSID, 0)).enqueue(new Callback<SySArtBean>() {
            @Override
            public void onResponse(Call<SySArtBean> call, Response<SySArtBean> response) {
                if (response.body().isResult()) {
                    titleText = response.body().getData().getInfo().getTitle();
                    setTitle(response.body().getData().getInfo().getTitle());
                    awesome.setSelected(response.body().getData().getInfo().getIs_thumbs() == 1);
                    collection.setSelected(response.body().getData().getInfo().getIs_collection() == 1);
                    content.loadDataWithBaseURL(null, response.body().getData().getInfo().getA_info(), "text/html", "UTF-8", null);
                    contentText= Html.fromHtml(response.body().getData().getInfo().getA_info()).toString();
                }
            }

            @Override
            public void onFailure(Call<SySArtBean> call, Throwable t) {

            }
        });
        getComment(1);
    }

    private void getComment(int order) {
        webApi.getComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeySYSID, 0), order).enqueue(new Callback<CommentBean>() {
            @Override
            public void onResponse(Call<CommentBean> call, Response<CommentBean> response) {
                if (response.body().isResult() && !isFinishing()) {
                    commentList.clear();
                    commentList.addAll(response.body().getData());
                    commentAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<CommentBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int resID, int position, int state) {
        switch (resID) {
            case R.id.text_comment_child_more:
                Intent intent = new Intent(this, CommentMoreActivity.class);
                intent.putExtra(CommentMoreActivity.KeyCommentID, commentList.get(position).getA_id());
                intent.putExtra(CommentMoreActivity.KeyArtID, getIntent().getIntExtra(KeySYSID, 0));
                startActivity(intent);
                break;
            case R.id.text_awesome:
                webApi.setAwesomeForComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeySYSID, 0), commentList.get(position).getA_id(), 0, state).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (!response.body().isResult()) {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_like:
                awesome.setSelected(!awesome.isSelected());
                webApi.setAwesome(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeySYSID, 0), collection.isSelected() ? 1 : 0).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (!response.body().isResult()) {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
            case R.id.view_share:
                ShareParams shareParams;
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setTitle(getTitleText());
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                shareParams.setImageData(bitmap);
                shareParams.setText("");
                shareParams.setUrl("http://www.ditouwang.net/");
                ShareUtil.share(this,shareParams);
                break;
            case R.id.view_collection:
                collection.setSelected(!collection.isSelected());
                webApi.setCollection(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeySYSID, 0), collection.isSelected() ? 1 : 0).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (!response.body().isResult()) {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });

                break;
            case R.id.sendButton:
                webApi.sendComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeySYSID, 0),editText.getText().toString()).enqueue(new Callback<CommonBean>() {
                @Override
                public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                    if (response.body().isResult()) {
                        showToast("评论成功");
                        onStart();
                        editText.setText(null);
                    } else {
                        showToast(response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<CommonBean> call, Throwable t) {

                }
            });
                break;
        }
    }

    @Override
    public boolean onEditorAction(final TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            findViewById(R.id.sendButton).callOnClick();
        }
        return false;
    }
}
